package rabbitmq_5;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.Scanner;
import java.util.UUID;
import java.util.concurrent.TimeoutException;

public class Consumer_5 {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory c = new ConnectionFactory();
        c.setHost("192.168.64.140");
        c.setPort(5672);
        c.setUsername("admin");
        c.setPassword("admin");

        Channel ch = c.newConnection().createChannel();
        //自定义交换机
        ch.exchangeDeclare("topic_logs", BuiltinExchangeType.TOPIC);
        //定义无参队列名
        String queue = ch.queueDeclare().getQueue();//默认无参创建队列参数为:不持久,队列名随机,自动删除,独占的
        //绑定陆游建
        System.out.println("请输入陆游建,多个以空格隔开:");
        String keys = new Scanner(System.in).nextLine();
        String[] str = keys.split("\\s+");
        for (String key:str){
            //绑定陆游建
            ch.queueBind(queue, "topic_logs", key);
        }

        DeliverCallback deliverCallback = new DeliverCallback() {
            @Override
            public void handle(String consumerTag, Delivery message) throws IOException {
               String string = new String(message.getBody());
               String key = message.getEnvelope().getRoutingKey();
                System.out.println("收到你的消息:"+string+"---"+key);
            }
        };

        CancelCallback cancelCallback = new CancelCallback() {
            @Override
            public void handle(String consumerTag) throws IOException {
            }
        };

        ch.basicConsume(queue, true, deliverCallback, cancelCallback);

    }

}
